﻿using System;
using System.Collections.Generic;

namespace RPCServer.Struct
{
    class Pool<Item> where Item : class
    {
        readonly Stack<Item> _items = new();
        Func<Item> _creater;

        public Pool(Func<Item> creater)
        {
            _creater = creater;
        }

        public virtual Item Take()
        {
            if (_items.Count < 1)
            {
                _items.Push(_creater());
            }
            return _items.Pop();
        }

        public virtual void Recycle(Item t)
        {
            _items.Push(t);
        }
    }
}
